import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'
import store from './store'
import { tokenUrl } from './valid/tokenUrl'
import 'lib-flexible/flexible'
import {
  Tabbar,
  TabbarItem,
  Search,
  Sidebar,
  SidebarItem,
  Toast,
  Tag,
  NavBar,
  Stepper,
  GoodsAction,
  GoodsActionIcon,
  GoodsActionButton,
  Form,
  Field,
  Button,
  Popup,
  Checkbox,
  SwipeCell,
  SubmitBar,
  Empty,
  Icon,
  AddressList,
  AddressEdit,
  Tab,
  Tabs,
  Dialog,
  Cell,
  Uploader,
  Swipe,
  Overlay,
  SwipeItem,
  List
} from 'vant';
//注册组件
Vue
  .use(List)
  .use(Tabbar)
  .use(TabbarItem)
  .use(Search)
  .use(Sidebar)
  .use(SidebarItem)
  .use(Toast)
  .use(Tag)
  .use(NavBar)
  .use(Stepper)
  .use(GoodsAction)
  .use(GoodsActionIcon)
  .use(GoodsActionButton)
  .use(Form)
  .use(Field)
  .use(Button)
  .use(Popup)
  .use(Checkbox)
  .use(SwipeCell)
  .use(SubmitBar)
  .use(Empty)
  .use(Icon)
  .use(AddressList)
  .use(AddressEdit)
  .use(Tabs)
  .use(Tab)
  .use(Dialog)
  .use(Cell)
  .use(Uploader)
  .use(Swipe)
  .use(SwipeItem)
  .use(Overlay)
//设置请求基础路径
axios.defaults.baseURL = 'https://apis.netstart.cn/music';
//添加一个axios请求之前拦截器
axios.interceptors.request.use(config => {
  //config: 请求配置(请求头, 请求参数, 请求类型, 请求路径, ....)
  // 

  // 
  let tokenString = localStorage.getItem('token');

  let appkey = 'U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=';

  //处理请求的公共参数
  if (config.method === 'get') {
    config.params = config.params || {};
    config.params.appkey = appkey;

    if (tokenUrl.indexOf(config.url) > -1) {
      config.params.tokenString = tokenString;
    }

  } else if (config.method === 'post') {
    config.data = config.data || {};

    if (tokenUrl.indexOf(config.url) > -1) {
      config.data.tokenString = tokenString;
    }

    //序列化post请求参数
    let dataParams = '';
    for (let key in config.data) {
      dataParams += `${key}=${config.data[key]}&`
    }
    dataParams += `appkey=${appkey}`;

    config.data = dataParams;

  }

  // //启动加载提示
  // Toast.loading({
  //   message: '加载中...',
  //   //禁止穿透点击
  //   forbidClick: true,
  //   loadingType: 'spinner',
  //   //不会自动关闭提示
  //   duration: 0
  // })



  return config;
}, err => {

  //关闭加载提示
  Toast.clear();
  return Promise.reject(err);
})

//添加一个响应之前拦截器
axios.interceptors.response.use(res => {
  //res: 服务器响应数据

  //关闭加载提示
  Toast.clear();

  return res;
}, err => {


  //关闭加载提示
  Toast.clear();

  return Promise.reject(err);
})

Vue.use(VueAxios, axios)

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
